package cc.leap.utils;

import org.noear.solon.data.cache.CacheService;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * CacheUtils
 *
 * @author leap
 * @date 2024/8/20
 */
public class CacheUtils {

    public static CacheService cacheService = SolonUtil.getBean(CacheService.class);

    public static <T> void setCacheObject(final String key, final T value) {
        cacheService.store(key, value, -1);
    }

    public static <T> void setCacheObject(final String key, final T value, final Integer timeout) {
        cacheService.store(key, value, timeout);
    }

    public static <T> T getCacheObject(final String key, Class<T> tClass) {
        return cacheService.get(key, tClass);
    }

    public static <T> List<T> getCacheObjectList(final String key, Class<T> tClass) {
        T t = cacheService.get(key, tClass);
        if (Objects.isNull(t)) {
            return Collections.emptyList();
        }
        return (List<T>) t;
    }

    public static void deleteObject(final String key) {
        cacheService.remove(key);
    }

}
